﻿Imports System.Data.OleDb
Public Class frmForgotPassword

#Region "NotifyIcon"

    Private Sub frmForgotPassword_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Dim ds As New DataSet()
    Dim intCurrentIndex As Integer = 0
    Dim da As New OleDbDataAdapter()
    Dim conn As New OleDbConnection()

    Dim strAccountType As String
    Dim strVerifyStaffID As String
    Private Sub frmForgotPassword_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmAccountMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmAccountMenu.pnlIcons)

        'Load ToolTips
        AccountToolTips()

        'Select from database
        conn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;" & _
            "Data Source=|DataDirectory|\SkyCoffee.mdb"

        da.SelectCommand = New OleDbCommand("SELECT * FROM tblAccounts")
        da.SelectCommand.Connection = conn

        da.UpdateCommand = New OleDbCommand("UPDATE tblAccounts SET fldAccountType = @fldAccountType, fldStaffUsername = @fldStaffUsername, fldStaffPassword = @fldStaffPassword WHERE fldStaffNumber = @fldStaffNumber")
        da.UpdateCommand.Connection = conn
        da.UpdateCommand.Parameters.Add("@fldAccountType", OleDbType.VarChar, 40, "fldAccountType")
        da.UpdateCommand.Parameters.Add("@fldStaffUsername", OleDbType.VarChar, 40, "fldStaffUsername")
        da.UpdateCommand.Parameters.Add("@fldStaffPassword", OleDbType.VarChar, 40, "fldStaffPassword")

        da.UpdateCommand.Parameters.Add("@fldStaffNumber", OleDbType.Numeric, 6, "fldStaffNumber")

        da.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then 'Check to see if the table is empty
            FillFields()
        Else
            MsgBox("No records exist in database")
        End If

    End Sub
    Public Sub FillFields()
        'Enter data into variables
        strAccountType = ds.Tables(0).Rows(intCurrentIndex).Item("fldAccountType").ToString()
        txtStaffID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffNumber").ToString()
        txtUsername.Text = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffUsername").ToString()

        strVerifyStaffID = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffNumber").ToString()
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click

        If IsNumeric(txtStaffID.Text) = False Or txtStaffID.Text.Length > 4 Or String.Compare(txtStaffID.Text, strVerifyStaffID) Then
            MsgBox("You have not entered a valid staff id")
            txtStaffID.Focus()
        ElseIf String.Compare(txtUsername.Text, txtVerifyUsername.Text) Then
            MsgBox("You have not entered a valid username")
            txtVerifyUsername.Focus()
        ElseIf txtVerifyUsername.Text = "" Or txtPassword.Text = "" Then
            MsgBox("Please fill in all of the fields")
        Else
            'Database Operations: Update Access Database
            Dim dr As DataRow

            dr = ds.Tables(0).Rows(intCurrentIndex) 'This gets a reference to the row currently being edited
            dr.BeginEdit()
            dr("fldAccountType") = strAccountType
            dr("fldStaffNumber") = txtStaffID.Text

            dr("fldStaffUsername") = txtUsername.Text
            dr("fldStaffPassword") = txtPassword.Text
            dr.EndEdit()

            da.Update(ds)  'Ask the dataadapter to call the UpdateCommand and update the database
            ds.AcceptChanges() 'Commits the change to the dataset.

            MsgBox("Password has been reset")
        End If
    End Sub

    Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
        If intCurrentIndex > 0 Then  'We move back only if we're not at the first row.
            intCurrentIndex = intCurrentIndex - 1  'Subtract one from the current index.
            FillFields()
        ElseIf ds.Tables(0).Rows.Count = 0 Then
            MsgBox("No records exist in database")
        Else
            MessageBox.Show("You are already at the first record.")
        End If
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then 'ds.Tables(0).Rows.Count - 1 is the index for the last row
            intCurrentIndex = intCurrentIndex + 1  'Add one to the current index.
            FillFields()
        ElseIf ds.Tables(0).Rows.Count = 0 Then
            MsgBox("No records exist in database")
        Else
            MessageBox.Show("You are already at the last record.")
        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        frmLogin.Show()
        Me.Close()
    End Sub
End Class